package com.property.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.property.biz.CommunityBiz;
import com.property.biz.ManagementBiz;
import com.property.biz.ProvCityDistBiz;
import com.property.entity.Community;
import com.property.entity.Management;
import com.property.entity.ProvCityDist;

@Controller
@RequestMapping("/main")
public class CommunityController {
	
	@Autowired
	private CommunityBiz communityBiz;
	@Autowired
	private ProvCityDistBiz provCityDistBiz;
	@Autowired
	private ManagementBiz managementBiz;
	
	@RequestMapping("/community")
	public String Community(Model model,HttpSession session){
		Management management=(Management) session.getAttribute("management");
		Management phone = managementBiz.getManById(management.getId());
		Community community=communityBiz.getById(management.getCommunity().getId());
		ProvCityDist dist=provCityDistBiz.distAll(community.getProvCityDistId());
		ProvCityDist city=provCityDistBiz.cityAll(dist.getCity_id());
		ProvCityDist prov=provCityDistBiz.provAll(city.getProvince_id());
		
		model.addAttribute("dist", dist);
		model.addAttribute("city", city);
		model.addAttribute("prov", prov);
		model.addAttribute("community", community);
		model.addAttribute("phone", phone);
		return "main/community";
	}
	@RequestMapping("/files")
	public String files(Community community,Management management,HttpSession session) throws IllegalStateException, IOException{
	
		String paString = session.getServletContext().getContextPath()+"\\statics\\images\\";
		
		System.out.println(paString);
		String sqlPath = null; 
		  //定义文件保存的本地路径
	      String localPath=session.getServletContext().getRealPath("statics\\images\\");
	      String localPathString="E:\\SS\\property\\src\\main\\webapp\\statics\\images\\";
			
	      System.out.println(localPath);
	      //定义 文件名

	      String filename=null;  
	      if(!community.getFile().isEmpty()){  
	          //生成uuid作为文件名称  
	          String uuid = UUID.randomUUID().toString().replaceAll("-",""); 
	          
	          
	          //获得文件类型（可以判断如果不是图片，禁止上传）  
	          String contentType=community.getFile().getContentType();  
	          
	          
	          //获得文件后缀名 
	          String suffixName=contentType.substring(contentType.indexOf("/")+1);
	          
	          //得到 文件名
	          filename=uuid+"."+suffixName; 
	          //文件保存路径
	          community.getFile().transferTo(new File(localPath+"\\"+filename));  
	          community.getFile().transferTo(new File(localPathString+"\\"+filename));
	      }
	      //把图片的相对路径保存至数据库
	      
	      sqlPath = "/images/"+filename;
	    
	      Management management2=(Management) session.getAttribute("management");
	      Management management3 = new Management();
	     
	      management3.setId(management2.getId());
	      management3.setPhone(management.getPhone());
	      Community community2=new Community();
	      community2.setId(management2.getCommunity().getId());
	      community2.setCommunityImg(sqlPath);
	      community2.setSecurityPhone(community.getSecurityPhone());
	      communityBiz.updateCommun(community2);
	      managementBiz.updateMan(management3);
	     


		return "redirect:community";
	}
	
	
}
